import 'dart:math';

import 'package:english_words/english_words.dart';
import 'package:flutter/material.dart';
import 'package:flutterdemo/library/sqflite/article_provider.dart';
import 'package:flutterdemo/library/sqflite/person_provider.dart';

class SqlitePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: PreferredSize(
        preferredSize: Size.fromHeight(54),
        child: AppBar(
          //标题居中
          centerTitle: true,
          //状态栏样式
          brightness: Brightness.light,
          title: Text(
            "sqflite",
            style: TextStyle(
              fontSize: 19,
              color: Colors.black38,
            ),
          ),
          elevation: 0,
          backgroundColor: Colors.white,
          leading: IconButton(
            icon: Icon(
              Icons.arrow_back,
              color: Colors.black,
            ),
            onPressed: () {
              Navigator.pop(context, 1);
            },
          ),
        ),
      ),
      body: Column(
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Text("person"),
              Text("article"),
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              RaisedButton(
                padding: EdgeInsets.all(5),
                child: Text('insert list '),
                onPressed: () {
                  PersonProvider().inserts(persons());
                },
              ),
              RaisedButton(
                padding: EdgeInsets.all(5),
                child: Text('insert list '),
                onPressed: () {
                  ArticleProvider().inserts(articles());
                },
              ),
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              RaisedButton(
                padding: EdgeInsets.all(5),
                child: Text('insert'),
                onPressed: () {
                  PersonProvider().insert(person());
                },
              ),
              RaisedButton(
                padding: EdgeInsets.all(5),
                child: Text('insert'),
                onPressed: () {
                  ArticleProvider().insert(article());
                },
              ),
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: <Widget>[
              RaisedButton(
                padding: EdgeInsets.all(5),
                child: Text('query'),
                onPressed: () {
                  PersonProvider().queryAll().then((value) {
                    value.forEach((element) {
                      print("【query person】${element.toMap()}");
                    });
                  });
                },
              ),
              RaisedButton(
                padding: EdgeInsets.all(5),
                child: Text('query'),
                onPressed: () {
                  ArticleProvider().queryAll().then((value) {
                    value.forEach((element) {
                      print("【query article】${element.toMap()}");
                    });
                  });
                },
              ),
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: <Widget>[
              RaisedButton(
                padding: EdgeInsets.all(5),
                child: Text('delete'),
                onPressed: () {
                  ArticleProvider().delete(10);
                },
              ),
              RaisedButton(
                padding: EdgeInsets.all(5),
                child: Text('delete'),
                onPressed: () {
                  ArticleProvider().delete(10);
                },
              ),
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: <Widget>[
              RaisedButton(
                padding: EdgeInsets.all(5),
                child: Text('clear'),
                onPressed: () {
                  ArticleProvider().clear();
                },
              ),
              RaisedButton(
                padding: EdgeInsets.all(5),
                child: Text('clear'),
                onPressed: () {
                  ArticleProvider().clear();
                },
              ),
            ],
          ),
        ],
      ),
    );
  }

  List<Person> persons() {
    List<Person> list = [];
    for (int i = 0; i < 1000; i++) {
      list.add(person());
    }
    return list;
  }

  Person person() {
    Random random = new Random();
    return new Person(
      random.nextInt(99999),
      generateWordPairs(random: random).elementAt(0).toString(),
      random.nextInt(120),
    );
  }

  Article article() {
    return new Article(
      title: generateWordPairs().elementAt(0).toString(),
      content: generateWordPairs().toString(),
    );
  }

  List<Article> articles() {
    List<Article> list = [];
    for (int i = 0; i < 1000; i++) {
      list.add(article());
    }
    return list;
  }
}
